using Circus.Syntax;
using System;
using System.Drawing;
using System.Globalization;
namespace Circus.CodeEditor.Serialization
{
	public class HtmlImporter : FmtImport
	{
		private const string htmlB = "b";
		private const string htmlI = "i";
		private const string htmlU = "u";
		private const string htmlStrike = "strike";
		private const string htmlStrong = "strong";
		private const string htmlFont = "font";
		private const string htmlColor = "color";
		private const string htmlBr = "br";
		private const string htmlP = "p";
		private const string htmlLt = "&lt;";
		private const string htmlQt = "&gt;";
		private const string htmlAmp = "&amp;";
		private const string htmlQuot = "&quot;";
		private const string htmlNbsp = "&nbsp;";
		private SyntaxParser parser = new HtmlParser();
		private string HtmlToString(string text)
		{
			return text.Replace("&lt;", "<").Replace("&gt;", ">").Replace("&amp;", "&").Replace("&quot;", "\"").Replace("&nbsp;", " ");
		}
		private int StringToHex(string text, int pos, int len)
		{
			int result;
			if (text.Length >= pos + len)
			{
				try
				{
					result = int.Parse(text.Substring(pos, len), NumberStyles.HexNumber);
					return result;
				}
				catch
				{
					result = 0;
					return result;
				}
			}
			result = 0;
			return result;
		}
		private Color HtmlToColor(string text)
		{
			Color result;
			if (text != string.Empty)
			{
				if (text[0] == '#')
				{
					result = Color.FromArgb(this.StringToHex(text, 1, 2), this.StringToHex(text, 3, 2), this.StringToHex(text, 5, 2));
				}
				else
				{
					result = Color.FromName(text);
				}
			}
			else
			{
				result = Color.Empty;
			}
			return result;
		}
		protected void WriteText(string text)
		{
			base.WriteString(this.HtmlToString(text));
		}
		protected void ProcessFontTag()
		{
			while (!this.parser.Eof)
			{
				switch (this.parser.NextToken())
				{
				case 2:
				case 3:
				case 4:
				case 5:
				case 6:
					return;
				case 9:
					if (string.Compare(this.parser.TokenString, "color", true) == 0)
					{
						if (this.parser.NextToken() == 7 && this.parser.NextToken() == 10)
						{
							base.WriteForeColor(this.HtmlToColor(this.parser.TokenString));
						}
					}
					break;
				}
			}
		}
		protected void ProcessTag(bool negate)
		{
			if (this.parser.NextToken() == 8)
			{
				string text = this.parser.TokenString.ToLower();
				if (!negate)
				{
					base.Push(text);
					string text2 = text;
					switch (text2)
					{
					case "br":
					case "p":
						base.WriteString("\r\n");
						break;
					case "b":
					case "strong":
						base.WriteFontStyle(FontStyle.Bold, true);
						break;
					case "i":
						base.WriteFontStyle(FontStyle.Italic, true);
						break;
					case "strike":
						base.WriteFontStyle(FontStyle.Strikeout, true);
						break;
					case "u":
						base.WriteFontStyle(FontStyle.Underline, true);
						break;
					case "font":
						this.ProcessFontTag();
						break;
					}
				}
				else
				{
					base.Pop(text);
				}
			}
		}
		protected override bool ReadContent()
		{
			bool result = base.ReadContent();
			this.parser.Strings = new StringList(this.reader);
			this.parser.Reset();
			int num = 0;
			int num2 = 0;
			while (!this.parser.Eof)
			{
				switch (this.parser.NextToken())
				{
				case 2:
					if (num != this.parser.CurrentPosition.Y && num2 != 6 && num2 != 4)
					{
						this.WriteText(' '.ToString() + this.parser.TokenString);
					}
					else
					{
						this.WriteText(this.parser.TokenString);
					}
					break;
				case 3:
					this.ProcessTag(false);
					break;
				case 5:
					this.ProcessTag(true);
					break;
				}
				num2 = this.parser.Token;
				num = this.parser.CurrentPosition.Y;
			}
			return result;
		}
		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (this.parser != null)
				{
					((IDisposable)this.parser).Dispose();
					this.parser = null;
				}
			}
			base.Dispose(disposing);
		}
	}
}
